home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Testing & Debugging / Fill Memory / Fill Memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-15  |  2.0 KB  |  71 lines  |  [TEXT/CWIE]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    Application that fills a large chunk of memory and then sees if that
  5. **    memory gets overwritten.  This is a simple debugging tool.
  6. **
  7. **    by Mark Cookson, Apple Developer Technical Support
  8. **
  9. **    File:    Fill Memory.h
  10. **
  11. **    Copyright ©1996 Apple Computer, Inc.
  12. **    All rights reserved.
  13. **
  14. **    You may incorporate this sample code into your applications without
  15. **    restriction, though the sample code has been provided "AS IS" and the
  16. **    responsibility for its operation is 100% yours.  However, what you are
  17. **    not permitted to do is to redistribute the source as "Apple Sample
  18. **    Code" after having made changes. If you're going to re-distribute the
  19. **    source, we require that you make it clear in the source that the code
  20. **    was descended from Apple Sample Code, but that you've made changes.
  21. */
  22.  
  23. #include <Dialogs.h>
  24. #include <Fonts.h>
  25. #include <Memory.h>
  26. #include <QuickDraw.h>
  27. #include <TextEdit.h>
  28. #include <Windows.h>
  29.  
  30. static void ToolBoxInit (void) {    
  31.     MaxApplZone();
  32.     InitGraf (&qd.thePort);
  33.     InitFonts ();
  34.     InitWindows ();
  35.     InitMenus ();
  36.     TEInit ();
  37.     InitDialogs ((long)nil);
  38.     InitCursor ();
  39.     return;
  40. }
  41.  
  42. void main (void) {
  43.     EventRecord        anEvent;
  44.     unsigned long *    theBuffer        = nil,
  45.                     spaceWanted        = 0,
  46.                     i                = 0;
  47.     Size            freeMem            = 0,
  48.                     growSize        = 0;
  49.  
  50.     ToolBoxInit ();
  51.     freeMem = MaxMem (&growSize);
  52.     spaceWanted = (freeMem - 1024) & ~3;
  53.     // save a K so the process manager can switch out of our app
  54.     // the & ~3 makes the memory allocated an integer number of longs (for speed)
  55.     theBuffer = (unsigned long *)NewPtr (spaceWanted);
  56.     if (!theBuffer || MemError ()) {
  57.         DebugStr ("\pCouldn't get memory");    // Where did the memory go?
  58.         return;
  59.     }
  60.  
  61.     for (i = 0; i < spaceWanted / 4; i++)    // Fill memory with our pattern
  62.         ((unsigned long *)theBuffer)[i] = 0xA5A5A5A5;
  63.  
  64.     while (anEvent.what != keyDown) {
  65.         WaitNextEvent (keyDownMask, &anEvent, 6, nil);
  66.         for (i = 0; i < spaceWanted / 4; i++) {
  67.             if (((unsigned long *)theBuffer)[i] != 0xA5A5A5A5)
  68.                 DebugStr ("\pFill Memory memory's got trashed!");
  69.         }
  70.     }
  71. }